﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessCommon.Responce;
using BusinessCommon.Request;
using DataAccess.Implementation;
using DataAccess;
using BusinessCommon.DTO;
using BusinessCommon.Utilities;
using System.Transactions;
using System.Data;
using DataAccess.Common;

namespace StoreWCF
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Service1 : ILoginService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserResponce LoginAction(UserRequest request)
        {
            UserResponce response = new UserResponce();
            bool isChangePass = false;
            using (var transaction = new TransactionScope())
            {
                try
                {
                    // Get user info
                    UserDto userDto = userDao.GetUserLogin(request.UserName, request.PasswordChange);

                    // Validate user info
                    if (userDto != null)
                    {
                        DateTime change_pwdate = userDto.Change_PWTime;
                        if (change_pwdate.AddDays(ItemDefineIF.MAX_PWDATE) < DateTime.Now)
                        {
                            isChangePass = true;
                            response.IsSuccess = true;
                            response.userDto = userDto;
                            response.ErrorCode = ItemDefineIF.ERR_00005;
                        }
                        else
                        {
                            response.IsSuccess = true;
                            response.userDto = userDto;
                        }
                    }
                    else
                    {
                        response.IsSuccess = false;
                        response.ErrorCode = ItemDefineIF.ERR_00001;
                    }

                    // Insert login log
                    if (response.IsSuccess && !isChangePass)
                    {
                        LOGIN_LOG_TBL login_log_tbl = new LOGIN_LOG_TBL();
                        login_log_tbl.LLG_TAN = userDto.User_ID;
                        login_log_tbl.LLG_LOG_DATE = DateTime.Now;
                        login_log_tbl.ADD_DATE = DateTime.Now;
                        login_log_tbl.ADD_PROG = this.ToString();
                        login_log_tbl.ADD_TAN = userDto.User_ID;
                        login_log_tbl.ENT_KBN = ItemDefineIF.ENT_KBN_USE;
                        if (!userDao.InsertLoginLog(login_log_tbl))
                        {
                            response.IsSuccess = false;
                            response.ErrorCode = "Loi insert sql";
                        }
                        else
                        {
                            response.IsSuccess = true;
                        }
                    }

                    // Mark transaction as complete
                    if (response.IsSuccess)
                    {
                        transaction.Complete();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    response.IsSuccess = false;
                    response.ErrorCode = "Loi thao tac DB";
                    return response;
                }
            };
            return response;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserResponce UpdatePwdAction(UserRequest request)
        {
            UserResponce response = new UserResponce();
            using (var transaction = new TransactionScope())
            {
                try
                {
                    // Get user info
                    UserDto userDto = userDao.GetUserLogin(request.UserName, request.Password);

                    // Validate user info
                    if (userDto != null)
                    {
                        TANTOU_MST TantouMst = new TANTOU_MST();
                        TantouMst.TAN_PWORD = request.PasswordChange;
                        TantouMst.TAN_CHANGE_PWDATE = DateTime.Now;
                        TantouMst.UPD_DATE = DateTime.Now;
                        TantouMst.UPD_PROG = this.ToString();
                        TantouMst.UPD_TAN = userDto.User_ID;
                        userDao.UpdatePassWord(TantouMst);
                    }
                    else
                    {
                        response.IsSuccess = false;
                        response.ErrorCode = ItemDefineIF.ERR_00011;
                    }

                    // Insert login log
                    if (response.IsSuccess)
                    {
                        LOGIN_LOG_TBL login_log_tbl = new LOGIN_LOG_TBL();
                        login_log_tbl.LLG_TAN = userDto.User_ID;
                        login_log_tbl.LLG_LOG_DATE = DateTime.Now;
                        login_log_tbl.ADD_DATE = DateTime.Now;
                        login_log_tbl.ADD_PROG = this.ToString();
                        login_log_tbl.ADD_TAN = userDto.User_ID;
                        login_log_tbl.ENT_KBN = ItemDefineIF.ENT_KBN_USE;
                        if (!userDao.InsertLoginLog(login_log_tbl))
                        {
                            response.IsSuccess = false;
                            response.ErrorCode = "Loi insert sql";
                        }
                        else
                        {
                            response.IsSuccess = true;
                        }
                    }

                    // Mark transaction as complete
                    if (response.IsSuccess)
                    {
                        transaction.Complete();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    response.IsSuccess = false;
                    response.ErrorCode = "Loi thao tac DB";
                }
            }
            return response;
        }
    }
}
